package com.meta.nd.filter.bo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.meta.nd.annotation.bo.FdDef;
import com.meta.nd.annotation.bo.NdDef;
import com.meta.nd.filter.vo.FilterFd;
import com.meta.nd.filter.vo.ResultFd;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


@Data
@NdDef("filterDefMvNd")
public class FilterDef implements Cloneable{
    @FdDef("code")
    String filterDefCode;
    @FdDef("name")
    String name;
    @FdDef("description")
    String description;
    @FdDef("ndCode")
    String targetNdCode;
    @FdDef("conditionNdCode")
    String conditionNdCode;
    @FdDef("resultNdCode")
    String resultNdCode;
    @FdDef("returnNdCode")
    String returnNdCode;
    @FdDef("conditionLayoutCode")
    String conditionLayoutCode;
    @FdDef("resultLayoutCode")
    String resultLayoutCode;
    @FdDef("detailLayoutCode")
    String detailLayoutCode;
    @FdDef("extCode")
    String extCode;//查询扩展代码
    @FdDef(ndName = "filterConditionFdNd")
    List<FilterFd> filterFds = new ArrayList<>();
    @FdDef(ndName = "filterResultFdNd")
    List<ResultFd> resultFds = new ArrayList<>();
    int startNo;
    int count;

    @JsonIgnore
    public List<FilterFd> getFilterFdWithFixedValue(){
        List<FilterFd> result = new ArrayList<>();
        for ( FilterFd filterFd : this.filterFds){
            if (!StringUtils.isEmpty(filterFd.getFixedValue()) || filterFd.isExpression())
                result.add(filterFd);
        }
        return result;
    }

    public FilterFd getFilterFdByFdAndAliasName(String fdAlias){
        List<FilterFd> result = new ArrayList<>();
        for ( FilterFd filterFd : this.filterFds){
            if (fdAlias.equals(filterFd.getFinalName())){
                result.add(filterFd);
            }
        }
        if ( result.size()>0)
            return result.get(0);
        else
            return null;
    }

    public void setDefault(){
        if (StringUtils.isEmpty(this.returnNdCode))
            this.returnNdCode = this.targetNdCode;
        if ( StringUtils.isEmpty(this.conditionNdCode))
            this.conditionNdCode = this.returnNdCode;
        if ( StringUtils.isEmpty(this.resultNdCode))
            this.resultNdCode = this.returnNdCode;
    }

    @Override
    public FilterDef clone(){
        try {
            FilterDef filterDef = (FilterDef)super.clone();
            filterDef.filterFds = new ArrayList<>();
            filterDef.filterFds.addAll(this.filterFds);
            filterDef.resultFds = new ArrayList<>();
            filterDef.resultFds.addAll(this.resultFds);
            return filterDef;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    public boolean containFilterFd(String aliasName){
        for( FilterFd filterFd : this.filterFds){
            if ( filterFd.getFinalName().equals(aliasName))
                return true;
        }
        return false;
    }

    @JsonIgnore
    public List<String> getResultNames(){
        List<String> fdNames = new ArrayList<>();
        for( ResultFd resultFd : this.resultFds )
            fdNames.add(resultFd.getFdName());
        return fdNames;
    }

    public List<String> getResultNames(String ndName){
        List<String> fdNames = new ArrayList<>();
        for( ResultFd resultFd : this.resultFds ) {
            if (ndName.equals(resultFd.getNdName()))
            fdNames.add(resultFd.getFdName());
        }
        return fdNames;
    }

    public void addFilterFd( FilterFd filterFd){
        this.filterFds.add(filterFd);
    }

    public void addResultFd(ResultFd resultFd){
        this.resultFds.add(resultFd);
    }

public FilterParamDef toFilterParamDef(){
    FilterParamDef filterParamDef = new FilterParamDef();
    filterParamDef.setFilterDefCode(this.filterDefCode);
    filterParamDef.setTargetNdCode(this.targetNdCode);
    filterParamDef.setReturnNdCode(this.returnNdCode);
    filterParamDef.setResultNdCode(this.resultNdCode);
    filterParamDef.setFilterFds(this.filterFds);
    filterParamDef.setResultFds(this.resultFds);
    filterParamDef.setStartNo(this.startNo);
    filterParamDef.setCount(this.count);
    return filterParamDef;
}

public void sortFd(){
        this.filterFds.sort((o1,o2)->{
            return o1.getSerNo() - o2.getSerNo();
        });
        this.resultFds.sort((o1,o2)->{
            return o1.getSerNo() - o2.getSerNo();
        });
}

}
